home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mint110s / dosmem.c < prev    next >
C/C++ Source or Header  |  1994-02-02  |  36KB  |  1,357 lines

  1. /*
  2. Copyright 1990,1991,1992 Eric R. Smith.
  3. Copyright 1992,1993 Atari Corporation.
  4. All rights reserved.
  5. */
  6.  
  7. /*
  8.  * GEMDOS emulation routines: these are for the GEMDOS system calls
  9.  * concerning allocating/freeing memory, including Pexec() (since
  10.  * this allocates memory) and Pterm() (since this, implicitly, frees
  11.  * it).
  12.  */
  13.  
  14. #include "mint.h"
  15.  
  16. #define DIRSEP(c) ((c) == '\\')
  17.  
  18. int procdate, proctime;    /* set when any processes are created/destroyed */
  19.  
  20. static long do_vfork P_((int));
  21.  
  22. /*
  23.  * new call for TT TOS, for the user to inform DOS of alternate memory
  24.  * FIXME: we really shouldn't trust the user so completely
  25.  * FIXME: doesn't work if memory protection is on
  26.  */
  27.  
  28. long ARGS_ON_STACK
  29. m_addalt(start, size)
  30.     long start, size;
  31. {
  32.     extern int no_mem_prot;        /* see main.c and memprot.c */
  33.  
  34.     if (!no_mem_prot) return 0;    /* pretend to succeed */
  35.     if (!add_region(alt, start, size, M_ALT))
  36.         return EINTRN;
  37.     else
  38.         return 0;
  39. }
  40.  
  41. /*
  42.  * internal routine for doing Malloc on a particular memory map
  43.  */
  44.  
  45. long
  46. _do_malloc(map, size, mode)
  47.     MMAP map;
  48.     long size;
  49.     int mode;
  50. {
  51.     virtaddr v;
  52.     MEMREGION *m;
  53.     long maxsize, mleft;
  54.  
  55.     if (size == -1L) {
  56.         maxsize = max_rsize(map);
  57.         if (curproc->maxmem) {
  58.             mleft = curproc->maxmem - memused(curproc);
  59.             if (maxsize > mleft)
  60.                 maxsize = mleft;
  61.             if (maxsize < 0)
  62.                 maxsize = 0;
  63.         }
  64.     /* make sure to round down */
  65.         return maxsize & ~MASKBITS;
  66.     }
  67.  
  68. /* special case: Malloc(0) should always return 0 */
  69.     if (size == 0)
  70.         return 0;
  71.  
  72.     if (curproc->maxmem) {        /* memory limit? */
  73.         if (size > curproc->maxmem - memused(curproc)) {
  74.             DEBUG(("malloc: memory request would exceed limit"));
  75.             return 0;
  76.         }
  77.     }
  78.  
  79.     m = get_region(map, size, mode);
  80.     if (!m) {
  81.         return 0;
  82.     }
  83.     v = attach_region(curproc, m);
  84.     if (!v) {
  85.         m->links = 0;
  86.         free_region(m);
  87.         return 0;
  88.     }
  89. /* NOTE: get_region returns a region with link count 1; since attach_region
  90.  * increments the link count, we have to remember to decrement the count
  91.  * to correct for this.
  92.  */
  93.     m->links--;
  94.     if ((mode & F_KEEP)) {    /* request for permanent memory */
  95.         m->mflags |= M_KEEP;
  96.     }
  97.     return (long)v;
  98. }
  99.  
  100. long ARGS_ON_STACK
  101. m_xalloc(size, mode)
  102.     long size;
  103.     int mode;
  104. {
  105.     long r, r1;
  106.     int protmode;
  107. #ifdef DEBUG_INFO
  108.     int origmode = mode;
  109. #endif
  110.  
  111.     TRACE(("Mxalloc(%ld,%x)",size,mode));
  112.  
  113. /*
  114.  * AKP: Hack here: if the calling process' PC is in ROM, then this is a
  115.  * Malloc call made by VDI's v_opnvwk routine.  So we change mode to
  116.  * include "super accessible."  This is temporary, until VDI catches up
  117.  * with multitasking TOS.
  118.  */
  119.  
  120.     if (((mode & F_PROTMODE) == 0) &&
  121.         (curproc->ctxt[SYSCALL].pc > 0x00e00000L) &&
  122.         (curproc->ctxt[SYSCALL].pc < 0x00efffffL)) {
  123.         mode |= (F_PROT_S + 0x10) | F_KEEP;
  124.         TRACE(("m_xalloc: VDI special (call from ROM)"));
  125.     }
  126. /*
  127.  * If the mode argument comes in a zero, then set it to the default
  128.  * value from prgflags.  Otherwise subtract one from it to bring it
  129.  * into line with the actual argument to alloc_region.
  130.  */
  131.     protmode = (mode & F_PROTMODE) >> F_PROTSHIFT;
  132.  
  133.     if (protmode == 0) {
  134.         protmode = (curproc->memflags & F_PROTMODE) >> F_PROTSHIFT;
  135.     }
  136.     else --protmode;
  137.  
  138.     if (protmode > PROT_MAX_MODE) {
  139.         DEBUG(("Mxalloc: invalid protection mode changed to private"));
  140.         protmode = PROT_P;
  141.     }
  142.  
  143. #if 0
  144. /* I'm very suspicious of the 0x08 flag; I can't see how it could
  145.  * work as the comment below seems to indicate -- ERS
  146.  */
  147.  
  148. /*
  149.  * if the mode argument has the 0x08 bit set then you're trying to change
  150.  * the protection mode of a block you already own. "size" is really its
  151.  * base address. (new as of 2/6/92)
  152.  */
  153.     if (mode & 0x08) change_prot_status(curproc,size,protmode);
  154. #endif
  155.  
  156.     /*
  157.      * Copy the F_KEEP attribute into protmode.  We didn't do that
  158.      * before now because change_prot_status don't want to see no
  159.      * steenking nofree attributes.
  160.      */
  161.  
  162.     protmode |= (mode & F_KEEP);
  163.  
  164.     /* mask off all but the ST/alternative RAM bits before further use */
  165.     mode &= 3;
  166.  
  167.     if (mode == 0) {
  168.         r = _do_malloc(core, size, protmode);
  169.         goto ret;
  170.     }
  171.     else if (mode == 1) {
  172.         r = _do_malloc(alt, size, protmode);
  173.         goto ret;
  174.     }
  175.     else if (size == -1) {
  176.         /* modes 2 and 3 are the same for for size -1 */
  177.         r = _do_malloc(core, -1L, PROT_P);
  178.         r1 = _do_malloc(alt, -1L, PROT_P);
  179.         if (r1 > r) r = r1;
  180.         goto ret;
  181.     }
  182.     else if (mode == 2) {
  183.         r = _do_malloc(core, size, protmode);
  184.         if (r == 0) r = _do_malloc(alt, size, protmode);
  185.         goto ret;
  186.     }
  187.     else /* if (mode == 3) */ {
  188.         r = _do_malloc(alt, size, protmode);
  189.         if (r == 0) r = _do_malloc(core, size, protmode);
  190.         goto ret;
  191.     }
  192. ret:
  193.     if (r == 0) {
  194.         DEBUG(("m_xalloc(%lx,%x) returns 0",size,origmode));
  195.     } else {
  196.         TRACE(("m_xalloc(%lx,%x) returns %lx",size,origmode,r));
  197.     }
  198.     return r;
  199. }
  200.  
  201. long ARGS_ON_STACK
  202. m_alloc(size)
  203.     long size;
  204. {
  205.     long r;
  206.  
  207.     TRACE(("Malloc(%lx)", size));
  208.     if (curproc->memflags & F_ALTALLOC)
  209.         r = m_xalloc(size, 3);
  210.     else
  211.         r = m_xalloc(size, 0);
  212.     TRACE(("Malloc: returning %lx", r));
  213.     return r;
  214. }
  215.  
  216. long ARGS_ON_STACK
  217. m_free(block)
  218.     virtaddr block;
  219. {
  220.     MEMREGION *m;
  221.     int i;
  222.  
  223.     TRACE(("Mfree(%lx)", block));
  224.     if (!block) {
  225.         DEBUG(("Mfree: null pointer"));
  226.         return EIMBA;
  227.     }
  228.  
  229. /* search backwards so that most recently allocated incarnations of
  230.    shared memory blocks are freed first (this doesn't matter very often)
  231.  */
  232.  
  233.     for (i = curproc->num_reg - 1; i >= 0; i--) {
  234.         if (curproc->addr[i] == block) {
  235.             m = curproc->mem[i];
  236.             assert(m != NULL);
  237.             assert(m->loc == (long)block);
  238.             curproc->mem[i] = 0;
  239.             curproc->addr[i] = 0;
  240.             m->links--;
  241.             if (m->links == 0) {
  242.                 free_region(m);
  243.             }
  244.             return 0;
  245.         }
  246.     }
  247.  
  248. /* hmmm... if we didn't find the region, perhaps it's a global
  249.  * one (with the M_KEEP flag set) belonging to a process that
  250.  * terminated
  251.  */
  252.     for (i = rootproc->num_reg - 1; i >= 0; i--) {
  253.         if (rootproc->addr[i] == block) {
  254.             m = rootproc->mem[i];
  255.             assert(m != NULL);
  256.             assert(m->loc == (long)block);
  257.             if (!(m->mflags & M_KEEP))
  258.                 continue;
  259.             TRACE(("Freeing M_KEPT memory"));
  260.             rootproc->mem[i] = 0;
  261.             rootproc->addr[i] = 0;
  262.             m->links--;
  263.             if (m->links == 0) {
  264.                 free_region(m);
  265.             }
  266.             return 0;
  267.         }
  268.     }
  269.  
  270.  
  271.     DEBUG(("Mfree: bad address %lx", block));
  272.     return EIMBA;
  273. }
  274.  
  275. long ARGS_ON_STACK
  276. m_shrink(dummy, block, size)
  277.     int dummy;
  278.     virtaddr block;
  279.     long size;
  280. {
  281.     MEMREGION *m;
  282.     int i;
  283.  
  284.     UNUSED(dummy);
  285.     TRACE(("Mshrink: %lx to %ld", block, size));
  286.     if (!block) {
  287.         DEBUG(("Mshrink: null pointer"));
  288.         return EIMBA;
  289.     }
  290.  
  291.     for (i = 0; i < curproc->num_reg; i++) {
  292.         if (curproc->addr[i] == block) {
  293.             m = curproc->mem[i];
  294.             assert(m != NULL);
  295.             assert(m->loc == (long)block);
  296.             return shrink_region(m, size);
  297.         }
  298.     }
  299.     DEBUG(("Mshrink: bad address (%lx)", block));
  300.     return EIMBA;
  301. }
  302.  
  303. long ARGS_ON_STACK
  304. p_exec(mode, ptr1, ptr2, ptr3)
  305.     int mode;
  306.     void *ptr1, *ptr2, *ptr3;
  307. {
  308.     MEMREGION *base,
  309.         *env = 0;    /* assignment suppresses spurious warning */
  310.     MEMREGION *text = 0;    /* for shared text regions */
  311.     PROC *p;
  312.     long r, flags = 0;
  313.     int i;
  314.     char mkbase = 0, mkload = 0, mkgo = 0, mkwait = 0, mkfree = 0;
  315.     char overlay = 0;
  316.     char thread = 0;
  317.     char ptrace;
  318.     char mkname = 0, *newname, *lastslash;
  319.     char localname[PNAMSIZ+1];
  320.     XATTR xattr;
  321.     int newpid;
  322.  
  323. #ifdef DEBUG_INFO
  324. /* tfmt and tail_offs are used for debugging only */
  325.     const char *tfmt = "Pexec(%d,%s,\"%s\",%lx)";
  326.     int tail_offs = 1;
  327. #endif
  328.  
  329. /* the high bit of mode controls process tracing */
  330.     switch(mode & 0x7fff) {
  331.     case 0:
  332.         mkwait = 1;        /* fall through */
  333.     case 100:
  334.         mkload = mkgo = mkfree = 1;
  335.         mkname = 1;
  336.         break;
  337.     case 200:            /* overlay current process */
  338.         mkload = mkgo = 1;
  339.         overlay = mkname = 1;
  340.         break;
  341.     case 3:
  342.         mkload = 1;
  343.         break;
  344.     case 6:
  345.         mkfree = 1;
  346.         /* fall through */
  347.     case 4:
  348.         mkwait = mkgo = 1;
  349.         thread = (mode == 4);
  350. #ifdef DEBUG_INFO
  351.         tfmt = "Pexec(%d,%lx,BP:%lx,%lx)";
  352.         tail_offs = 0;
  353. #endif
  354.         break;
  355.     case 106:
  356.         mkfree = 1;        /* fall through */
  357.     case 104:
  358.         thread = (mode == 104);
  359.         mkgo = 1;
  360.         mkname = (ptr1 != 0);
  361. #ifdef DEBUG_INFO
  362.         tfmt = "Pexec(%d,%s,BP:%lx,%lx)";
  363.         tail_offs = 0;
  364. #endif
  365.         break;
  366.     case 206:
  367. #if 0
  368.     /* mkfree has no effect when overlay is set, since
  369.      * in this case the "parent" and "child" are the same
  370.      * process; since the "child" will run in memory that the
  371.      * "parent" allocated, we don't want that memory freed!
  372.      */
  373.         mkfree = 1;
  374. #endif
  375.         /* fall through */
  376.     case 204:
  377.         mkgo = overlay = 1;
  378.         mkname = (ptr1 != 0);
  379. #ifdef DEBUG_INFO
  380.         tfmt = "Pexec(%d,%s,BP:%lx,%lx)";
  381.         tail_offs = 0;
  382. #endif
  383.         break;
  384.     case 7:
  385.         flags = (long)ptr1;    /* set program flags */
  386.         if (((flags & F_PROTMODE) >> F_PROTSHIFT) > PROT_MAX_MODE) {
  387.             DEBUG (("Pexec: invalid protection mode changed to private"));
  388.             flags = (flags & ~F_PROTMODE) | F_PROT_P;
  389.         }
  390.                     /* and fall through */
  391.     case 5:
  392.         mkbase = 1;
  393. #ifdef DEBUG_INFO
  394.         tfmt = "Pexec(%d,%lx,%s,%lx)";
  395.         tail_offs = 0;
  396. #endif
  397.         break;
  398.     default:
  399.         DEBUG(("Pexec(%d,%lx,%lx,%lx): bad mode",mode,ptr1,ptr2,ptr3));
  400.         return EINVFN;
  401.     }
  402.  
  403.     TRACE((tfmt,mode,ptr1,(char *)ptr2+tail_offs,ptr3));
  404.  
  405. /* Pexec with mode 0x8000 indicates tracing should be active */
  406.     ptrace = (!mkwait && (mode & 0x8000));
  407.  
  408. /* in most cases, we'll want a process struct to exist,
  409.  * so make sure one is around. Note that we must be
  410.  * careful to free it later!
  411.  */
  412.  
  413. TRACE(("Checking for memory for new PROC structure"));
  414.     p = 0;
  415.     if (!overlay) {
  416.         p = new_proc();
  417.         if (!p) {
  418.             DEBUG(("Pexec: couldn't get a PROC struct"));
  419.             return ENSMEM;
  420.         }
  421.     }
  422.  
  423. TRACE(("creating environment"));
  424.  
  425.     if (mkload || mkbase) {
  426.         env = create_env((char *)ptr3, flags);
  427.         if (!env) {
  428.             DEBUG(("Pexec: unable to create environment"));
  429.             if (p) dispose_proc(p);
  430.             return ENSMEM;
  431.         }
  432.     }
  433.  
  434. TRACE(("creating base page"));
  435.  
  436.     if (mkbase) {
  437.         base = create_base((char *)ptr2, env, flags, 0L);
  438.         if (!base) {
  439.             DEBUG(("Pexec: unable to create basepage"));
  440.             detach_region(curproc, env);
  441.             if (p) dispose_proc(p);
  442.             return ENSMEM;
  443.         }
  444. TRACELOW(("Pexec: basepage region(%lx) is %ld bytes at %lx", base, base->len, base->loc));
  445.     }
  446.     else if (mkload) {
  447.         base = load_region((char *)ptr1, env, (char *)ptr2,
  448.             &xattr, &text, &flags);
  449.         if (!base) {
  450.             DEBUG(("Pexec: load_region failed"));
  451.             detach_region(curproc, env);
  452.             if (p) dispose_proc(p);
  453.             return mint_errno;
  454.         }
  455. TRACE(("Pexec: basepage region(%lx) is %ld bytes at %lx", base, base->len, base->loc));
  456.     }
  457.     else {    /* mode == 4,6,104,106,204, or 206 -- just go */
  458.         base = addr2mem((virtaddr)ptr2);
  459.         if (base)
  460.             env = addr2mem(*(void **)(base->loc + 0x2c));
  461.         else
  462.             env = 0;
  463.         if (!env) {
  464.             DEBUG(("Pexec: memory not owned by parent"));
  465.             if (p) dispose_proc(p);
  466.             return EIMBA;
  467.         }
  468. #if 0
  469.       /* make sure that the PC we are about to use is in a region which is
  470.        * attached to the process; this is most commonly a problem for
  471.        * shared text segment programs.
  472.        * BUG: we should verify that the PC is in a region to which the
  473.        * child process should legitimately have access.
  474.        */
  475.               text = addr2region(((BASEPAGE *)base->loc)->p_tbase);
  476.               if (text == base) {
  477.               /* text segment is part of base region */
  478.                       text = NULL;
  479.               }
  480. #endif
  481.     }
  482.  
  483. /* make a local copy of the name, in case we are overlaying the current
  484.  * process
  485.  */
  486.     if (mkname) {
  487.         lastslash = 0;
  488.         newname = ptr1;
  489.         while (*newname) {
  490.             if (*newname == '\\' || *newname == '/')
  491.                 lastslash = newname;
  492.             ++newname;
  493.         }
  494.         if (!lastslash)
  495.             lastslash = ptr1;
  496.         else
  497.             lastslash++;
  498.  
  499.         i = 0; newname = localname;
  500.         while (i++ < PNAMSIZ) {
  501.             if (*lastslash == '.' || *lastslash == 0) {
  502.                 *newname = 0; break;
  503.             }
  504.             else
  505.                 *newname++ = *lastslash++;
  506.         }
  507.         *newname = 0;
  508.     }
  509.  
  510.     if (mkload || mkbase) {
  511.         /*
  512.          * Now that the file's loaded, flags is set to the prgflags
  513.          * for the file.  In the case of mkbase it's been right all along.
  514.          * Here's where we change the protection on the environment to
  515.          * match those flags.
  516.          */
  517.         mark_region(env,(short)((flags & F_PROTMODE) >> F_PROTSHIFT));
  518.     }
  519.  
  520.     if (p) {
  521.     /* free the PROC struct so fork_proc will succeed */
  522.     /* FIXME: it would be much better to pass the PROC as a parameter
  523.      * to fork_proc!!
  524.      */
  525.         dispose_proc(p);
  526.         p = 0;
  527.     }
  528.  
  529.     if (mkgo) {
  530.         BASEPAGE *b;
  531.  
  532.     /* tell the child who the parent was */
  533.         b = (BASEPAGE *)base->loc;
  534.  
  535.         if (overlay) {
  536.             b->p_parent = curproc->base->p_parent;
  537.             p = curproc;
  538.         /* make sure that exec_region doesn't free the base and env */
  539.             base->links++;
  540.             env->links++;
  541.             if (text) text->links++;
  542.         }
  543.         else {
  544.             b->p_parent = curproc->base;
  545.             p = fork_proc();
  546.         }
  547.         if (!p) {
  548.             if (mkbase) {
  549.                 detach_region(curproc, base);
  550.                 detach_region(curproc, env);
  551.                 if (text) detach_region(curproc, text);
  552.             }
  553.             return mint_errno;
  554.         }
  555.  
  556.     /* jr: add Pexec information to PROC struct */
  557.         strncpy(p->cmdlin, b->p_cmdlin, 128);
  558.         p->fname[0] = 0;
  559.         if (mkload) {
  560.             char tmp[PATH_MAX];
  561.             char *source = ptr1;
  562.             tmp[1] = ':';
  563.             if (source[1] == ':') {
  564.                 tmp[0] = source[0];
  565.                 source += 2;
  566.             } else
  567.                 tmp[0] = 'A' + curproc->curdrv;
  568.             if (DIRSEP(source[0]))    /* absolute path? */
  569.             {
  570.                 strncpy (&tmp[2], &source[0], PATH_MAX-2);
  571.                 strcpy (p->fname, tmp);
  572.             } else {
  573.                 if (! d_getcwd (&tmp[2], tmp[0] - 'A' + 1, PATH_MAX - 2))
  574.                     ksprintf (p->fname, "%s\\%s", tmp, source);
  575.             }
  576.         }
  577.  
  578.         if (ptrace)
  579.             p->ptracer = pid2proc(p->ppid);
  580.  
  581.     /* Even though the file system won't allow unauthorized access
  582.      * to setuid/setgid programs, it's better to err on the side of
  583.      * caution and forbid them to be traced (since the parent can arrange
  584.      * to share the child's address space, not all accesses need to
  585.      * go through the file system.)
  586.      */
  587.         if (mkload && mkgo && !p->ptracer) {    /* setuid/setgid is OK */
  588.             if (xattr.mode & S_ISUID)
  589.                 p->euid = xattr.uid;
  590.             if (xattr.mode & S_ISGID)
  591.                 p->egid = xattr.gid;
  592.         }
  593.     /* exec_region frees the memory attached to p; that's always what
  594.      * we want, since fork_proc duplicates the memory, and since
  595.      * if we didn't call fork_proc then we're overlaying.
  596.      * NOTE: after this call, we may not be able to access the
  597.      * original address space that the Pexec was taking place in
  598.      * (if this is an overlaid Pexec, we just freed that memory).
  599.      */
  600.         (void)exec_region(p, base, thread);
  601.         attach_region(p, env);
  602.         attach_region(p, base);
  603.         if (text) attach_region(p, text);
  604.  
  605.         if (mkname) {
  606.     /* interesting coincidence -- if a process needs a name, it usually
  607.      * needs to have its domain reset to DOM_TOS. Doing it this way
  608.      * (instead of doing it in exec_region) means that Pexec(4,...)
  609.      * can be used to create new threads of execution which retain
  610.      * the same domain.
  611.      */
  612.             if (!thread)
  613.                 p->domain = DOM_TOS;
  614.  
  615.     /* put in the new process name we saved above */
  616.             strcpy(p->name, localname);
  617.         }
  618.  
  619.     /* turn on tracing for the new process */
  620.         if (p->ptracer)
  621.             p->ctxt[CURRENT].ptrace = 1;
  622.  
  623.     /* set the time/date stamp of u:\proc */
  624.         proctime = timestamp;
  625.         procdate = datestamp;
  626.  
  627.         if (overlay) {
  628.             /* correct for temporary increase in links (see above) */
  629.             base->links--;
  630.             env->links--;
  631.             if (text) text->links--;
  632.             /* let our parent run, if it Vfork'd() */
  633.             if ( (p = pid2proc(curproc->ppid)) != 0 ) {
  634.                 if (p->wait_q == WAIT_Q && 
  635.                     p->wait_cond == (long)curproc) {
  636.                     short sr = spl7();
  637.                     rm_q(WAIT_Q, p);
  638.                     add_q(READY_Q, p);
  639.                     spl(sr);
  640.                 }
  641.             }
  642.  
  643.         /* OK, let's run our new code */
  644.         /* we guarantee ourselves at least 2 timeslices to do an Mshrink */
  645.             assert(curproc->magic == CTXT_MAGIC);
  646.             fresh_slices(2);
  647.             leave_kernel();
  648.             change_context(&(curproc->ctxt[CURRENT]));
  649.         }
  650.         else {
  651.     /* we want this process to run ASAP */
  652.     /* so we temporarily give it high priority and put it first on the
  653.      * run queue
  654.      */
  655.             run_next(p, 2);
  656.         }
  657.     }
  658.  
  659.     if (mkfree) {
  660.         detach_region(curproc, base);
  661.         detach_region(curproc, env);
  662.         if (text) detach_region(curproc, text);
  663.     }
  664.  
  665.     if (mkwait) {
  666.         long oldsigint, oldsigquit;
  667.  
  668.         oldsigint = curproc->sighandle[SIGINT];
  669.         oldsigquit = curproc->sighandle[SIGQUIT];
  670.         curproc->sighandle[SIGINT] =
  671.              curproc->sighandle[SIGQUIT] = SIG_IGN;
  672.  
  673.         newpid = p->pid;
  674.         for(;;) {
  675.             r = p_wait3(0, (long *)0);
  676.             if (r < 0) {
  677.                 ALERT("p_exec: wait error");
  678.                 return EINTRN;
  679.             }
  680.             if ( newpid == ((r&0xffff0000L) >> 16) ) {
  681.                 TRACE(("leaving Pexec; child return code %ld", r));
  682.                 r = r & 0x0000ffffL;
  683.                 break;
  684.             }
  685.             if (curproc->pid)
  686.                 DEBUG(("Pexec: wrong child found"));
  687.         }
  688.         curproc->sighandle[SIGINT] = oldsigint;
  689.         curproc->sighandle[SIGQUIT] = oldsigquit;
  690.         return r;
  691.     }
  692.     else if (mkgo) {
  693.     /* warning: after the yield() the "p" structure may not exist any more
  694.      * (if the child exits right away)
  695.      */
  696.         newpid = p->pid;
  697.         yield();    /* let the new process run */
  698.         return newpid;
  699.     } else {
  700.         TRACE(("leaving Pexec with basepage address %lx", base->loc));
  701.         return base->loc;
  702.     }
  703. }
  704.  
  705. /*
  706.  * terminate a process, with return code "code". If que == ZOMBIE_Q, free
  707.  * all resources attached to the child; if que == TSR_Q, free everything
  708.  * but memory.
  709.  * NOTE: terminate() should be called only when the process is to be
  710.  * "terminated with extreme prejuidice". Most times, p_term or p_termres
  711.  * are the functions to use, since they allow the user to do some cleaning
  712.  * up, etc.
  713.  */
  714.  
  715. long
  716. terminate(code, que)
  717.     int code, que;
  718. {
  719.     extern PROC *dlockproc[];    /* in dosdir.c */
  720.     PROC *p;
  721.     FILEPTR *fp;
  722.     MEMREGION *m;
  723.     MEMREGION **hold_mem;
  724.     virtaddr *hold_addr;
  725.     int  i, wakemint = 0;
  726.     DIR *dirh, *nexth;
  727.     extern short bconbsiz;    /* in bios.c */
  728.  
  729.     if (bconbsiz)
  730.         (void) bflush();
  731.  
  732.     assert(que == ZOMBIE_Q || que == TSR_Q);
  733.  
  734.     if (curproc->pid == 0) {
  735.         FATAL("attempt to terminate MiNT");
  736.     }
  737.  
  738. /* cancel all pending timeouts for this process */
  739.     cancelalltimeouts();
  740. /* cancel alarm clock */
  741.     curproc->alarmtim = 0;
  742.  
  743. /* release any drives locked by Dlock */
  744.     for(i = 0; i < NUM_DRIVES; i++) {
  745.         if (dlockproc[i] == curproc) {
  746.             dlockproc[i] = 0;
  747.             changedrv(i);
  748.         }
  749.     }
  750.  
  751. /* release the controlling terminal, if we're a process group leader */
  752.     fp = curproc->handle[-1];
  753.     if (fp && is_terminal(fp) && curproc->pgrp == curproc->pid) {
  754.         struct tty *tty = (struct tty *)fp->devinfo;
  755.         if (curproc->pgrp == tty->pgrp)
  756.             tty->pgrp = 0;
  757.     }
  758.  
  759. /* close all files */
  760.     for (i = MIN_HANDLE; i < MAX_OPEN; i++) {
  761.         if ((fp = curproc->handle[i]) != 0)
  762.             do_close(fp);
  763.         curproc->handle[i] = 0;
  764.     }
  765.  
  766. /* close any unresolved Fsfirst/Fsnext directory searches */
  767.     for (i = 0; i < NUM_SEARCH; i++) {
  768.         if (curproc->srchdta[i]) {
  769.             DIR *dirh = &curproc->srchdir[i];
  770.             (*dirh->fc.fs->closedir)(dirh);
  771.             release_cookie(&dirh->fc);
  772.             dirh->fc.fs = 0;
  773.         }
  774.     }
  775.  
  776. /* close pending opendir/readdir searches */
  777.     for (dirh = curproc->searches; dirh; ) {
  778.         if (dirh->fc.fs) {
  779.             (*dirh->fc.fs->closedir)(dirh);
  780.             release_cookie(&dirh->fc);
  781.         }
  782.         nexth = dirh->next;
  783.         kfree(dirh);
  784.         dirh = nexth;
  785.     }
  786.  
  787. /* release the directory cookies held by the process */
  788.     for (i = 0; i < NUM_DRIVES; i++) {
  789.         release_cookie(&curproc->curdir[i]);
  790.         curproc->curdir[i].fs = 0;
  791.         release_cookie(&curproc->root[i]);
  792.         curproc->root[i].fs = 0;
  793.     }
  794.  
  795. /* release all semaphores owned by this process */
  796.     free_semaphores(curproc->pid);
  797.  
  798. /* free all memory */
  799. /* if mflags & M_KEEP then attach it to process 0 */
  800.     if (que == ZOMBIE_Q) {
  801.         for (i = curproc->num_reg - 1; i >=0; i--) {
  802.             m = curproc->mem[i];
  803.             curproc->mem[i] = 0; curproc->addr[i] = 0;
  804.             if (m) {
  805.         /* don't free specially allocated memory */
  806.                 if (m->mflags & M_KEEP) {
  807.                     if (curproc != rootproc)
  808.                         attach_region(rootproc, m);
  809.                 }
  810.                 m->links--;
  811.                 if (m->links == 0) {
  812.                     free_region(m);
  813.                 }
  814.             }
  815.         }
  816.  
  817.         /*
  818.          * mark the mem & addr arrays as void so the memory
  819.          * protection code won't try to walk them. Do this before
  820.          * freeing them so we don't try to walk them when marking
  821.          * those pages themselves as free!
  822.          *
  823.          * Note: when a process terminates, the MMU root pointer
  824.          * still points to that process' page table, until the next
  825.          * process is dispatched.  This is OK, since the process'
  826.          * page table is in system memory, and it isn't going to be
  827.          * freed.  It is going to wind up on the free process list,
  828.          * though, after dispose_proc. This might be Not A Good
  829.          * Thing.
  830.          */
  831.  
  832.         hold_addr = curproc->addr;
  833.         hold_mem = curproc->mem;
  834.  
  835.         curproc->mem = NULL;
  836.         curproc->addr = NULL;
  837.         curproc->num_reg = 0;
  838.  
  839.         kfree(hold_addr);
  840.         kfree(hold_mem);
  841.     }
  842. /*    else
  843.          make TSR process non-swappable */
  844.  
  845. /*
  846.  * make sure that any open files that refer to this process are
  847.  * closed
  848.  */
  849.     changedrv(PROC_RDEV_BASE | curproc->pid);
  850.  
  851. /* find our parent (if parent not found, then use process 0 as parent
  852.  * since that process is constantly in a wait loop)
  853.  */
  854.  
  855.     p = pid2proc(curproc->ppid);
  856.     if (!p) {
  857.         TRACE(("terminate: parent not found"));
  858.         p = pid2proc(0);
  859.     }
  860.  
  861. /* NOTE: normally just post_sig is sufficient for sending a signal; but
  862.  * in this particular case, we have to worry about processes that are
  863.  * blocking all signals because they Vfork'd and are waiting for us to
  864.  * finish (which is indicated by a wait_cond matching our PROC
  865.  * structure), and also processes that are ignoring SIGCHLD but are
  866.  * waiting for us.
  867.  */
  868.     if (p->wait_q == WAIT_Q && 
  869.         (p->wait_cond == (long)curproc || p->wait_cond == (long)p_waitpid) ) {
  870.         short sr = spl7();
  871.         TRACE(("terminate: waking up parent"));
  872.         rm_q(WAIT_Q, p);
  873.         add_q(READY_Q, p);
  874.         spl(sr);
  875.     }
  876.     if (curproc->ptracer && curproc->ptracer != p) {
  877.         /* BUG: should we ensure curproc->ptracer is awake ? */
  878.         post_sig(curproc->ptracer, SIGCHLD);    /* tell tracing process */
  879.     }
  880.     post_sig(p, SIGCHLD);        /* inform of process termination */
  881.  
  882. /* find our children, and orphan them
  883.  * also, check for processes we were tracing, and
  884.  * cancel the trace
  885.  */
  886.     i = curproc->pid;
  887.     for (p = proclist; p; p = p->gl_next) {
  888.         if (p->ppid == i) {
  889.             p->ppid = 0;    /* have the system adopt it */
  890.             if (p->wait_q == ZOMBIE_Q) 
  891.                 wakemint = 1;    /* we need to wake proc. 0 */
  892.         }
  893.         if (p->ptracer == curproc) {
  894.             p->ptracer = 0;
  895. /*
  896.  * `FEATURE': we terminate traced processes when the tracer terminates.
  897.  * It might plausibly be argued that it would be better to let them
  898.  * continue, to let some (new) tracer take them over. On the other hand,
  899.  * if the tracer terminated normally, it should have used Fcntl(PTRACESFLAGS)
  900.  * to reset the trace nicely, so something must be wrong for us to have
  901.  * reached here.
  902.  */
  903.             post_sig(p, SIGTERM);    /* arrange for termination */
  904.         }
  905.     }
  906.  
  907.     if (wakemint) {
  908.         p = rootproc;        /* pid 0 */
  909.         if (p->wait_q == WAIT_Q) {
  910.             short sr = spl7();
  911.             rm_q(WAIT_Q, p);
  912.             add_q(READY_Q, p);
  913.             spl(sr);
  914.         }
  915.     }
  916.  
  917. /* this makes sure that our children are inherited by the system;
  918.  * plus, it may help avoid problems if somehow a signal gets
  919.  * through to us
  920.  */
  921.     for(i = 0; i < NSIG; i++)
  922.         curproc->sighandle[i] = SIG_IGN;
  923.  
  924. /* finally, reset the time/date stamp for u:\proc */
  925.     proctime = timestamp;
  926.     procdate = datestamp;
  927.  
  928.     sleep(que, (long)(unsigned)code);
  929.  
  930. /* we shouldn't ever get here */
  931.     FATAL("terminate: sleep woke up when it shouldn't have");
  932.     return 0;
  933. }
  934.  
  935. /*
  936.  * TOS process termination entry points:
  937.  * p_term terminates the process, freeing its memory
  938.  * p_termres lets the process hang around resident in memory, after
  939.  * shrinking its transient program area to "save" bytes
  940.  */
  941.  
  942. long ARGS_ON_STACK
  943. p_term(code)
  944.     int code;
  945. {
  946.     CONTEXT *syscall;
  947.  
  948.     TRACE(("Pterm(%d)", code));
  949. /* call the process termination vector */
  950.     syscall = &curproc->ctxt[SYSCALL];
  951.  
  952.     if (syscall->term_vec != (long)rts) {
  953.         TRACE(("term_vec: user has something to do"));
  954. /*
  955.  * we handle the termination vector just like Supexec(), by
  956.  * sending signal 0 to the process. See supexec() in xbios.c for details.
  957.  * Note that we _always_ want to unwind the signal stack, and setting
  958.  * bit 1 of curproc->sigmask tells handle_sig to do that -- see signal.c.
  959.  */
  960.         curproc->sigmask |= 1L;
  961.         (void)supexec((Func)syscall->term_vec, 0L, 0L, 0L, 0L, 
  962.                 (long)code);
  963. /*
  964.  * if we arrive here, continue with the termination...
  965.  */
  966.     }
  967.     return terminate(code, ZOMBIE_Q);
  968. }
  969.  
  970. long ARGS_ON_STACK
  971. p_term0()
  972. {
  973.     return p_term(0);
  974. }
  975.  
  976. long ARGS_ON_STACK
  977. p_termres(save, code)
  978.     long save;
  979.     int code;
  980. {
  981.     MEMREGION *m;
  982.     int i;
  983.  
  984.     TRACE(("Ptermres(%ld, %d)", save, code));
  985.     m = curproc->mem[1];    /* should be the basepage (0 is env.) */
  986.     if (m) {
  987.         (void)shrink_region(m, save);
  988.     }
  989. /*
  990.  * make all of the TSR's private memory globally accessible;
  991.  * this means that more TSR's will "do the right thing"
  992.  * without having to have prgflags set.
  993.  */
  994.     for (i = 0; i < curproc->num_reg; i++) {
  995.         m = curproc->mem[i];
  996.         if (m && m->links == 1) {    /* only the TSR is owner */
  997.             if (get_prot_mode(m) == PROT_P) {
  998.                 mark_region(m, PROT_G);
  999.             }
  1000.         }
  1001.     }
  1002.     return terminate(code, TSR_Q);
  1003. }
  1004.  
  1005. /*
  1006.  * routine for waiting for children to die. Return has the pid of the
  1007.  * found child in the high word, and the child's exit code in
  1008.  * the low word. If no children exist, return "File Not Found".
  1009.  * If (nohang & 1) is nonzero, then return a 0 immediately if we have
  1010.  * no dead children but some living ones that we still have to wait
  1011.  * for. If (nohang & 2) is nonzero, then we return any stopped
  1012.  * children; otherwise, only children that have exited or are stopped
  1013.  * due to a trace trap are returned.
  1014.  * If "rusage" is non-zero and a child is found, put the child's
  1015.  * resource usage into it (currently only the user and system time are
  1016.  * sent back).
  1017.  * The pid argument specifies a set of child processes for which status
  1018.  * is requested:
  1019.  *     If pid is equal to -1, status is requested for any child process.
  1020.  *
  1021.  *    If pid is greater than zero, it specifies the process ID of a
  1022.  *    single child process for which status is requested.
  1023.  *
  1024.  *    If pid is equal to zero, status is requested for any child
  1025.  *    process whose process group ID is equal to that of the calling
  1026.  *    process.
  1027.  *
  1028.  *    If pid is less than -1, status is requested for any child process
  1029.  *    whose process group ID is equal to the absolute value of pid.
  1030.  *
  1031.  * Note this call is a real standard crosser... POSIX.1 doesn't have the
  1032.  * rusage stuff, BSD doesn't have the pid stuff; both are useful, so why
  1033.  * not have it all!
  1034.  */
  1035.  
  1036. long ARGS_ON_STACK
  1037. p_waitpid(pid, nohang, rusage)
  1038.     int pid;
  1039.     int nohang;
  1040.     long *rusage;
  1041. {
  1042.     long r;
  1043.     PROC *p, *q;
  1044.     int ourpid;
  1045.     int found;
  1046.  
  1047.     TRACE(("Pwaitpid(%d, %d, %lx)", pid, nohang, rusage));
  1048.     ourpid = curproc->pid;
  1049.  
  1050. /* if there are terminated children, clean up and return their info;
  1051.  * if there are children, but still running, wait for them;
  1052.  * if there are no children, return an error
  1053.  */
  1054.  
  1055.     do {
  1056. /* look for any children */
  1057.         found = 0;
  1058.         for (p = proclist; p; p = p->gl_next) {
  1059.             if ((p->ppid == ourpid || p->ptracer == curproc) &&
  1060.                 (pid == -1 ||
  1061.                 (pid > 0 && pid == p->pid) ||
  1062.                 (pid == 0 && p->pgrp == ourpid) ||
  1063.                 (pid < -1 && p->pgrp == -pid))) {
  1064.                 found++;
  1065.                 if (p->wait_q == ZOMBIE_Q || p->wait_q == TSR_Q)
  1066.                     break;
  1067.  
  1068. /* p->wait_cond == 0 if a stopped process has already been waited for */
  1069.                 if (p->wait_q == STOP_Q && p->wait_cond) {
  1070.                     if ((nohang & 2) ||
  1071.                     ((p->wait_cond&0x1f00) == (SIGTRAP<<8)))
  1072.                         break;
  1073.                 }
  1074.             }
  1075.         }
  1076.         if (!p) {
  1077.             if (found) {
  1078.                 if (nohang & 1)
  1079.                     return 0;
  1080.                 if (curproc->pid)
  1081.                     TRACE(("Pwaitpid: going to sleep"));
  1082.                 sleep(WAIT_Q, (long)p_waitpid);
  1083.             }
  1084.             else {
  1085.                 DEBUG(("Pwaitpid: no children found"));
  1086.                 return EFILNF;
  1087.             }
  1088.         }
  1089.     } while (!p);
  1090.  
  1091. /* OK, we've found our child */
  1092. /* calculate the return code from the child's exit code and pid */
  1093.     r = (((unsigned long)p->pid) << 16) | (p->wait_cond & 0x0000ffff);
  1094.  
  1095. /* check resource usage */
  1096.     if (rusage) {
  1097.         *rusage++ = p->usrtime;
  1098.         *rusage = p->systime;
  1099.     }
  1100.  
  1101. /* avoid adding adopted trace processes usage to the foster parent */
  1102.     if (curproc->pid == p->ppid) {
  1103.     /* add child's resource usage to parent's */
  1104.         if (p->wait_q == TSR_Q || p->wait_q == ZOMBIE_Q) {
  1105.             curproc->chldstime += p->systime;
  1106.             curproc->chldutime += p->usrtime;
  1107.         }
  1108.     }
  1109.  
  1110. /* if it was stopped, mark it as having been found and again return */
  1111.     if (p->wait_q == STOP_Q) {
  1112.         p->wait_cond = 0;
  1113.         return r;
  1114.     }
  1115.  
  1116. /* We have to worry about processes which attach themselves to running
  1117.  * processes which they want to trace. We fix things up so that the
  1118.  * second time the signal gets delivered we will go all the way to the
  1119.  * end of this function.
  1120.  */
  1121.      if (p->ptracer && p->ptracer->pid != p->ppid) {
  1122.         if (curproc == p->ptracer) {
  1123.         /* deliver the signal to the tracing process first */
  1124.             TRACE(("Pwaitpid(ptracer): returning status to tracing process"));
  1125.             p->ptracer = NULL;
  1126.             return r;
  1127.         }
  1128.         else {
  1129.         /* Hmmm, the real parent got here first */
  1130.             TRACE(("Pwaitpid(ptracer): returning status to parent process"));
  1131.             p->ppid = -1;
  1132.             return r;
  1133.         }
  1134.     }
  1135.     
  1136. /* if it was a TSR, mark it as having been found and return */
  1137.     if (p->wait_q == TSR_Q) {
  1138.         p->ppid = -1;
  1139.         return r;
  1140.     }
  1141.  
  1142. /* it better have been on the ZOMBIE queue from here on in... */
  1143.     assert(p->wait_q == ZOMBIE_Q);
  1144.     assert(p != curproc);
  1145.  
  1146. /* take the child off both the global and ZOMBIE lists */
  1147.     { short sr = spl7();
  1148.     rm_q(ZOMBIE_Q, p);
  1149.     spl(sr);
  1150.     }
  1151.  
  1152.     if (proclist == p) {
  1153.         proclist = p->gl_next;
  1154.         p->gl_next = 0;
  1155.     }
  1156.     else {
  1157.         q = proclist;
  1158.         while(q && q->gl_next != p)
  1159.             q = q->gl_next;
  1160.         assert(q);
  1161.         q->gl_next = p->gl_next;
  1162.         p->gl_next = 0;
  1163.     }
  1164.  
  1165.     dispose_proc(p);    /* free the PROC structure */
  1166.  
  1167.     return r;
  1168. }
  1169.  
  1170. /* p_wait3: BSD process termination primitive, here to maintain
  1171.  * compatibility with existing binaries.
  1172.  */
  1173. long ARGS_ON_STACK
  1174. p_wait3(nohang, rusage)
  1175.     int nohang;
  1176.     long *rusage;
  1177. {
  1178.     return p_waitpid(-1, nohang, rusage);
  1179. }
  1180.  
  1181. /* p_wait: block until a child has exited, and don't worry about
  1182.    resource stats. this is provided as a convenience, and to maintain
  1183.    compatibility with existing binaries (yes, I'm lazy...). we could
  1184.    make do with Pwaitpid().
  1185.  */
  1186.  
  1187. long ARGS_ON_STACK
  1188. p_wait()
  1189. {
  1190. /*
  1191.  * BEWARE:
  1192.  * POSIX says that wait() should be implemented as
  1193.  * Pwaitpid(-1, 0, (long *)0). Pwait is really not
  1194.  * useful for much at all, but we'll keep it around
  1195.  * for a while (with it's old, crufty semantics)
  1196.  * for backwards compatibility. People implementing
  1197.  * POSIX style libraries should use Pwaitpid even
  1198.  * to implement wait().
  1199.  */
  1200.     return p_wait3(2, (long *)0);
  1201. }
  1202.  
  1203. /*
  1204.  * do_vfork(save): create a duplicate of  the current process. This is
  1205.  * essentially a vfork() algorithm, except that if (save == 1) the
  1206.  * parent's address space is saved, and then restored when the process
  1207.  * is made runnable again. The parent is suspended until either the child
  1208.  * process (the duplicate) exits or does a Pexec which overlays its
  1209.  * memory space.
  1210.  *
  1211.  * "txtsize" is the size of the process' TEXT area, if it has a valid one;
  1212.  * this is part of the second memory region attached (the basepage one)
  1213.  * and need not be saved (we assume processes don't write on their own
  1214.  * code segment)
  1215.  */
  1216.  
  1217. static long
  1218. do_vfork(save)
  1219.     int save;
  1220. {
  1221.     PROC *p;
  1222.     long sigmask;
  1223.     MEMREGION *m, *savemem = 0;
  1224.     long savesize, txtsize;
  1225.     int i, newpid;
  1226.     char *saveplace;
  1227.  
  1228.     p = fork_proc();
  1229.     if (!p) {
  1230.         DEBUG(("do_vfork: couldn't get new PROC struct"));
  1231.         return mint_errno;
  1232.     }
  1233. /* set u:\proc time+date */
  1234.     proctime = timestamp;
  1235.     procdate = datestamp;
  1236.  
  1237. /*
  1238.  * maybe save the parent's address space
  1239.  */
  1240.     txtsize = p->txtsize;
  1241.  
  1242.     if (save) {
  1243.         TRACE(("do_vfork: saving parent"));
  1244.         savesize = memused(curproc) - txtsize;
  1245.         assert(savesize >= 0);
  1246.  
  1247.         saveplace = (char *)alloc_region(alt, savesize, PROT_P);
  1248.         if (!saveplace)
  1249.             saveplace = (char *)alloc_region(core, savesize, PROT_P);
  1250.  
  1251.         if (!saveplace) {
  1252.             DEBUG(("do_vfork: can't save parent's memory"));
  1253.             p->ppid = 0;        /* abandon the child */
  1254.             post_sig(p, SIGKILL);    /* then kill it */
  1255.             p->ctxt[CURRENT].pc = (long)check_sigs;
  1256.                 /* just to make sure it dies */
  1257.             p->ctxt[CURRENT].ssp = (long)(p->stack + ISTKSIZE);
  1258.             p->pri = MAX_NICE+1;
  1259.             run_next(p, 1);
  1260.             yield();
  1261.             return ENSMEM;
  1262.         }
  1263.         savemem = addr2mem((virtaddr)saveplace);
  1264.         assert(savemem);
  1265.         for (i = 0; i < curproc->num_reg; i++) {
  1266.             m = curproc->mem[i];
  1267.             if (m && m != savemem && !(m->mflags & M_SHTEXT)) {
  1268.                 if (i != 1 || txtsize == 0) {
  1269.                     quickmove(saveplace, (char *)m->loc, m->len);
  1270.                     saveplace += m->len;
  1271.                 }
  1272.                 else {
  1273.                     quickmove(saveplace, (char *)m->loc+txtsize,
  1274.                     m->len - txtsize);
  1275.                     saveplace += m->len - txtsize;
  1276.                 }
  1277.             }
  1278.         }
  1279.     }
  1280.                 
  1281.     p->ctxt[CURRENT] = p->ctxt[SYSCALL];
  1282.     p->ctxt[CURRENT].regs[0] = 0;    /* child returns a 0 from call */
  1283.     p->ctxt[CURRENT].sr &= ~(0x2000); /* child must be in user mode */
  1284. #if 0        /* set up in fork_proc() */
  1285.     p->ctxt[CURRENT].ssp = (long)(p->stack + ISTKSIZE);
  1286. #endif
  1287.  
  1288. /* watch out for job control signals, since our parent can never wake
  1289.  * up to respond to them. solution: block them; exec_region (in mem.c)
  1290.  * clears the signal mask, so an exec() will unblock them.
  1291.  */
  1292.     p->sigmask |= (1L << SIGTSTP) | (1L << SIGTTIN) | (1L << SIGTTOU);
  1293.  
  1294.     TRACE(("do_vfork: parent going to sleep, wait_cond == %lx",
  1295.         (long)p));
  1296.  
  1297. /* WARNING: This sleep() must absolutely not wake up until the child
  1298.  * has released the memory space correctly. That's why we mask off
  1299.  * all signals.
  1300.  */
  1301.     sigmask = curproc->sigmask;
  1302.     curproc->sigmask = ~((unsigned long)1 << SIGKILL);
  1303.  
  1304.     { short sr = spl7();
  1305.     add_q(READY_Q, p);        /* put it on the ready queue */
  1306.     sleep(WAIT_Q, (long)p);            /* while we wait for it */
  1307.     spl(sr);
  1308.     }
  1309.     TRACE(("do_vfork: parent waking up"));
  1310.  
  1311.     if (save) {
  1312.         TRACE(("do_vfork: parent restoring memory"));
  1313.         saveplace = (char *)savemem->loc;
  1314.         for (i = 0; i < curproc->num_reg; i++) {
  1315.             m = curproc->mem[i];
  1316.             if (m && (m != savemem) && !(m->mflags & M_SHTEXT)) {
  1317.                 if (i != 1 || txtsize == 0) {
  1318.                     quickmove((char *)m->loc, saveplace, m->len);
  1319.                     saveplace += m->len;
  1320.                 }
  1321.                 else {
  1322.                     quickmove((char *)m->loc+txtsize, saveplace,
  1323.                     m->len - txtsize);
  1324.                     saveplace += m->len - txtsize;
  1325.                 }
  1326.             }
  1327.         }
  1328.         detach_region(curproc, savemem);
  1329.     }
  1330.     curproc->sigmask = sigmask;
  1331. /* note that the PROC structure pointed to by p may be freed during
  1332.  * the check_sigs call!
  1333.  */
  1334.     newpid = p->pid;
  1335.     check_sigs();    /* did we get any signals while sleeping? */
  1336.     return newpid;
  1337. }
  1338.  
  1339. /*
  1340.  * here are the interfaces that the user sees. Pvfork() doesn't save
  1341.  * the child's address space; Pfork() does. Someday Pfork() should
  1342.  * allow asynchronous execution of both child and parent, but this
  1343.  * will do for now.
  1344.  */
  1345.  
  1346. long ARGS_ON_STACK
  1347. p_vfork()
  1348. {
  1349.     return do_vfork(0);
  1350. }
  1351.  
  1352. long ARGS_ON_STACK
  1353. p_fork()
  1354. {
  1355.     return do_vfork(1);
  1356. }
  1357.